<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$dbh;

function dbConnect($dsn)
{
    global $dbh;
    $dbh = sqlite_open($dsn) or die('can\'t connect to DB.');
}

function dbQuery($sql,$data=array())
{
    global $dbh;
    return sqlite_query($dbh, dbPrepareQuery($sql, $data));
}

function dbInsert($sql,$data=array())
{
    global $dbh;
    dbQuery($sql,$data);
    return sqlite_last_insert_rowid($dbh);
}

function dbFetchAll($sql,$data=array())
{
    return sqlite_fetch_all(dbQuery($sql,$data), SQLITE_ASSOC);
}

function dbFetchOne($sql,$data=array())
{
    return sqlite_fetch_array(dbQuery($sql,$data), SQLITE_ASSOC);
}

function dbPrepareQuery($sql,$data)
{    
    if(count($data)==0) return $sql;
    
    foreach($data as $col=>$value)
    {
        $value = sqlite_escape_string($value);
        $sql = str_replace($col, $value, $sql);
    }
 
    return $sql;
}

function dbClose()
{
    global $dbh;
    sqlite_close($dbh);
}
function dbDropDatabase($dsn)
{
    unlink($dsn);
}
?>
